package cn.mayaslight.common.http;

import cn.mayaslight.common.chain.IChain;
import cn.mayaslight.common.exception.HandleException;
import cn.mayaslight.common.springbean.APICaller;

public class HttpDataTypeJudgement<T> extends IChain {

	private RequestBuilderPack<T> requestBuilder;
	private StringBuilder stringBuilder;
	
	public HttpDataTypeJudgement(RequestBuilderPack<T> requestBuilder, StringBuilder stringBuilder) {
		this.requestBuilder = requestBuilder;
		this.stringBuilder = stringBuilder;
	}

	@Override
	public boolean doThis() throws Exception {
		
		if(stringBuilder.toString().startsWith(APICaller.HTTP_URL_JSON_PREFIX)){
			stringBuilder.delete(0, APICaller.HTTP_URL_JSON_PREFIX.length());
			requestBuilder.setType(APICaller.JSON);
		}else if(stringBuilder.toString().startsWith(APICaller.HTTP_URL_XML_PREFIX)){
			stringBuilder = stringBuilder.delete(0, APICaller.HTTP_URL_XML_PREFIX.length());
			requestBuilder.setType(APICaller.XML);
		}else{
			throw new HandleException("数据类型必须为JSON或者XML!");
		}
		
		return true;
	}

	@Override
	public void doIfCompleteThis() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFinal() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void doIfCompleteAll() throws Exception {
		// TODO Auto-generated method stub

	}

}
